package testHutools.testDateUtils;

import cn.hutool.core.date.*;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author keyer
 * @Name : hutools.DateUtils.TestMul
 * @Description : 描述
 * Created by cyd on 2021-07-05 15:33:42
 */
public class DateUtilTest {

    /**
     * 时间间隔
     */
    @Test
    public void test01() {
        Date now = new Date();
        DateTime dateTime = DateUtil.endOfWeek(now);
        System.out.println(dateTime);
        long between = dateTime.between(now, DateUnit.HOUR);
        System.out.println(between);
    }

    /**
     * 时间偏移
     */
    @Test
    public void test02(){
        Date date = new Date();
        DateTime threeMonthBefore = DateUtil.offsetMonth(date, -3);
        System.out.println(threeMonthBefore);
    }

    /**
     * 将字符串时间转化为date
     */
    @Test
    public void parse() {
        String time = "2019-07-23 18:00:00";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date dateTime = null;
        try {
            dateTime = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(dateTime);// 此时不是标准格式
        String result = simpleDateFormat.format(dateTime);
        System.out.println(result);
    }

    /**
     * 不标准的时间转换
     */
    @Test
    public void test()  {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
        Date date = new Date();
        String dateStr = format.format(date);
        System.out.println(dateStr);

        Date newDate = null;
        try {
            newDate = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(newDate);

        Date dateTime = endOfHour(newDate);
        System.out.println(dateTime);
    }

    public static DateTime endOfHour(Date date) {
        return new DateTime(endOfHour(calendar(date)));
    }

    public static Calendar endOfHour(Calendar calendar) {
        return ceiling(calendar, DateField.HOUR);
    }

    public static Calendar ceiling(Calendar calendar, DateField dateField) {
        return DateModifier.modify(calendar, dateField.getValue(), DateModifier.ModifyType.CEILING);
    }

    public static Calendar calendar(Date date) {
        if (date instanceof DateTime) {
            return ((DateTime) date).toCalendar();
        } else {
            return calendar(date.getTime());
        }
    }

    public static Calendar calendar(long millis) {
        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(millis);
        return cal;
    }

    @Test
    public void testBeginAndEnd(){
        Date now = new Date();

        DateTime offset = DateUtil.offset(now, DateField.HOUR_OF_DAY, -2);
        Date beginOfDay = DateUtil.beginOfDay(offset);
        Date endOfDay01 = DateUtil.endOfDay(beginOfDay);
        System.out.println(endOfDay01);
        DateTime endOfDay02 = DateUtil.endOfDay(offset);
        System.out.println(endOfDay02);
    }
}
